//
#include "OtherCtrl.h"
//
//*****************************************************************************
//!\brief	Check the rotate direction  
//!\param	None
//!\retval  None                                                                  
//*****************************************************************************
void DirRotate(void)
{static uCHAR dir_rot = 0;
//
    if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pins_7) == Bit_SET)
    {
        if(dir_rot == 0)
        {
            dir_rot = 1;
            //
            delay.dir_rot= 0;
            timeout.dir_rot= 0;
            g_DirRotCnt = 0;
        }
        //
        delay.dir_rot= 1;
        if(timeout.dir_rot)
        {
            g_ForRevDirUpdate = 0;  // forward rotation
        }
    }
    else
    {
        if(dir_rot == 1)
        {
            dir_rot = 0;
            //
            delay.dir_rot= 0;
            timeout.dir_rot= 0;
            g_DirRotCnt = 0;
        }
        //
        delay.dir_rot= 1;
        if(timeout.dir_rot)
        {
            g_ForRevDirUpdate = 1;  // reverse rotation
        }
    }
}

//*****************************************************************************
//!\brief	Scan Key1 and Key2
//!\param	None
//!\retval  None                                                                  
//*****************************************************************************
void KeyScan(void)
{   // key1
    if(!flag.key1)
    {
        if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pins_11) == Bit_RESET)
        {
            delay.key1= 1;
            if(timeout.key1)
            {
                flag.key1 = 1;
                //
                flag.key1_fun = 1;
            }
        }
        else
        {
            delay.key1= 0;
            timeout.key1= 0;
            g_Key1Cnt = 0;
        }
    }
    else
    {
        if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pins_11) == Bit_SET)
        {
            delay.key1= 1;
            if(timeout.key1)
            {
                flag.key1 = 0;
            }
        }
        else
        {
            delay.key1= 0;
            timeout.key1= 0;
            g_Key1Cnt = 0;
        }
    }
    // key2
    if(!flag.key2)
    {
        if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pins_12) == Bit_RESET)
        {
            delay.key2= 1;
            if(timeout.key2)
            {
                flag.key2 = 1;
                //
                flag.key2_fun = 1;
            }
        }
        else
        {
            delay.key2= 0;
            timeout.key2= 0;
            g_Key2Cnt = 0;
        }
    }
    else
    {
        if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pins_12) == Bit_SET)
        {
            delay.key2= 1;
            if(timeout.key2)
            {
                flag.key2 = 0;
            }
        }
        else
        {
            delay.key2= 0;
            timeout.key2= 0;
            g_Key2Cnt = 0;
        }
    }
}

//*****************************************************************************
//!\brief	The LED control logic 
//!\param	None
//!\retval  None
//*****************************************************************************
void LED_Ctrl(void)
{static uCHAR  flash = 0;
//
    delay.led = 1;
    g_DelayTimeLED = LED_TIME;
    if(timeout.led)
    {
        timeout.led = 0;
        g_CntLED = 0;
        //
        flash = ~flash;
    }
    //
    if(protect.bitprt.acp)
    {
        if(flash)
        {
            LED1_OFF;
            LED2_OFF;
            LED3_ON;
        }
        else
        {
            LED1_OFF;
            LED2_OFF;
            LED3_OFF;
        }
    }
    else if(protect.bitprt.ocp)
    {
        if(flash)
        {
            LED1_ON;
            LED2_OFF;
            LED3_ON;
        }
        else
        {
            LED1_OFF;
            LED2_OFF;
            LED3_OFF;
        }
    }
    else if(protect.bitprt.lrcp || protect.bitprt.lrpp)
    {
        if(flash)
        {
            LED1_OFF;
            LED2_ON;
            LED3_ON;
        }
        else
        {
            LED1_OFF;
            LED2_OFF;
            LED3_OFF;
        }
    }
    else if(protect.bitprt.uvp || protect.bitprt.ovp)
    {
        if(flash)
        {
            LED1_ON;
            LED2_ON;
            LED3_OFF;
        }
        else
        {
            LED1_OFF;
            LED2_OFF;
            LED3_OFF;
        }
    }
    else if(protect.bitprt.otp)
    {
        if(flash)
        {
            LED1_ON;
            LED2_ON;
            LED3_ON;
        }
        else
        {
            LED1_OFF;
            LED2_OFF;
            LED3_OFF;
        }
    }
    else
    {
        switch(g_Stage)
        {
            case NONE_STAGE:
                LED1_OFF;
                LED2_OFF;
                LED3_OFF;
                break;
            case FIRST_STAGE:
                LED1_ON;
                LED2_OFF;
                LED3_OFF;
                break;
            case SECOND_STAGE:
                LED1_ON;
                LED2_ON;
                LED3_OFF;
                break;  
            default:
                LED1_OFF;
                LED2_OFF;
                LED3_OFF;
                break;
        }
    }
}

//=========================================================================================

